home *** CD-ROM | disk | FTP | other *** search
- #include <mac.h>
- #include "config.h"
- #include "rc.h"
-
- long gMudLibDirID;
- short gMudLibVolRef;
- long gMudResponseFactor;
-
- #undef fopen
- #undef open
-
- #define DIR_FLAG 0x10
-
- extern char *string_copy(char *str);
-
- /*
- * Very weak replacement for crypt().
- */
-
- #define rot(v,c) (((v) << c) | ((v) >> (8-c)))
-
- static int mirror(int x)
- {
- return ((x & 1) << 7) | ((x & 2) << 5) | ((x & 4) << 3) | ((x & 8) << 1) |
- ((x & 16) >> 1) | ((x & 32) >> 3) | ((x & 64) >> 5) | ((x & 128) >> 7);
- }
-
- char *crypt(char *pw, char *salt)
- {
- static char e64[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz.0123456789";
- static char enc[9];
- char tp[9];
- int scrm,i,bpos,n;
-
- memset(tp,'\0',8);
- strncpy(tp,pw,8);
- tp[0] ^= enc[0] = salt[0];
- tp[3] ^= enc[1] = salt[1];
- scrm = 165;
- for (i = 7; i >= 0; i--) scrm = (tp[i] ^= scrm >> 1);
- for (i = 7; i; i--) tp[tp[i-1] & 7] ^= rot(tp[i],1);
- for (i = (tp[0] & 127)+19; i; i--)
- tp[tp[i & 7] & 7] ^= rot(tp[(tp[i & 7] >> 3) & 7],tp[i & 7] >> 6);
- for (i = 3; i >= 0; i--) tp[i] ^= mirror(tp[7-i]);
- bpos = 0;
- for (i = 7; i > 1; i--) {
- n = ((tp[bpos >> 3] >> (bpos & 7)) | (tp[(bpos >> 3)+1] << (8-(bpos & 7)))) & 63;
- enc[i] = e64[n];
- bpos += 6;
- }
- enc[8] = '\0';
- return enc;
- }
-
- int random(void)
- {
- return (unsigned short)Random();
- }
-
- char *parsefname(const char *fname, char *namebuff, char *mud_lib)
- {
- short i, j = 0;
- Boolean slash = false;
-
- if (mud_lib) {
- j = strlen(MUD_LIB);
- strcpy(namebuff, MUD_LIB);
- }
- if (*fname != '/')
- namebuff[j++] = ':';
- for (i = 0; i < strlen(fname); i++) {
- if (fname[i] == '/') {
- if (!slash) {
- namebuff[j++] = ':';
- slash = true;
- }
- } else if (fname[i] != ':') {
- slash = false;
- namebuff[j++] = fname[i];
- }
- }
- namebuff[j] = '\0';
- return namebuff;
- }
-
- int macopen (const char *fname, int mode)
- {
- char namebuff[64];
-
- return open(parsefname(fname, namebuff, MUD_LIB), mode);
- }
-
- FILE *macfopen (const char *fname, const char *mode)
- {
- char namebuff[64];
-
- return fopen(parsefname(fname, namebuff, MUD_LIB), mode);
- }
-
- int macrename(const char *oldname, const char *newname)
- {
- char newnamebuff[64];
- char oldnamebuff[64];
-
- remove(parsefname(newname, newnamebuff, MUD_LIB));
- return rename(parsefname(oldname, oldnamebuff, MUD_LIB), newnamebuff);
- }
-
- int macremove(const char *filename)
- {
- char namebuff[64];
-
- return remove(parsefname(filename, namebuff, MUD_LIB));
- }
-
- int stat(char *fname, struct stat *buf) {
- Str255 pstr;
- CInfoPBRec pb;
-
- parsefname(fname, pstr, NULL);
- c2pstr(pstr);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = pstr;
- pb.hFileInfo.ioFDirIndex = 0;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = gMudLibDirID;
- pb.hFileInfo.ioResult = noErr;
- if (PBGetCatInfo(&pb, false) != noErr) {
- errno = ENOENT;
- return -1;
- }
- buf->st_mode = S_IFREG;
- if (pb.hFileInfo.ioFlAttrib & 0x10)
- buf->st_mode = S_IFDIR;
- buf->st_size = pb.hFileInfo.ioFlLgLen;
- buf->st_mtime = pb.hFileInfo.ioFlMdDat;
- return 0;
- }
-
- DirList *macgetdir(char *pathname)
- {
- CInfoPBRec pb;
- short i, x;
- Str255 fName;
- short index = 1;
- long directoryID;
- char namebuff[64];
- DirList *dirList = NULL;
- Boolean atleastone = false;
-
- directoryID = gMudLibDirID;
- pb.hFileInfo.ioResult = noErr;
- if (pathname[0] != '.') {
- for (i = 0, x = 0; i < strlen(pathname); i++) {
- if (pathname[i] == '/') {
- namebuff[0] = i - x;
- memcpy(&namebuff[1], &pathname[x], namebuff[0]);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = namebuff;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = directoryID;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = 0;
- if (PBGetCatInfo(&pb, false) != noErr || !(pb.hFileInfo.ioFlAttrib & DIR_FLAG)) {
- return NULL;
- }
- directoryID = pb.hFileInfo.ioDirID;
- x = i + 1;
- atleastone = true;
- }
- }
- if (!atleastone) {
- i = strlen(pathname);
- }
- namebuff[0] = i - x;
- memcpy(&namebuff[1], &pathname[x], namebuff[0]);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = namebuff;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = directoryID;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = 0;
- if (PBGetCatInfo(&pb, false) != noErr || !(pb.hFileInfo.ioFlAttrib & DIR_FLAG)) {
- return NULL;
- }
- directoryID = pb.hFileInfo.ioDirID;
- }
-
- while (pb.hFileInfo.ioResult == noErr) {
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = fName;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = directoryID;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = index;
-
- if (PBGetCatInfo(&pb, false) == noErr) {
- DirList *dirEntry;
-
- fName[*fName + 1] = '\0';
- dirEntry = (DirList *)NewPtr(sizeof(DirList));
- dirEntry->name = string_copy(&fName[1]);
- dirEntry->next = dirList;
- dirList = dirEntry;
- }
- index++;
- }
- return dirList;
- }
-
- int maccountdir(DirList *dirlist)
- {
- int count;
-
- for (count = 0; dirlist; dirlist = dirlist->next, count++)
- ;
- return count;
- }
-
- void macfreedir(DirList *dirlist)
- {
- DirList *dlist;
-
- while (dirlist) {
- dlist = dirlist->next;
- DisposPtr((Ptr)dirlist);
- dirlist = dlist;
- }
- }
-
- static char fnamebuff[64];
-
- char *finddir(char *pathname, long *directoryID)
- {
- CInfoPBRec pb;
- short i, x;
- Boolean atleastone = false;
-
- *directoryID = gMudLibDirID;
- for (i = 0, x = 0; i < strlen(pathname); i++) {
- if (pathname[i] == '/') {
- fnamebuff[0] = i - x;
- memcpy(&fnamebuff[1], &pathname[x], fnamebuff[0]);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = fnamebuff;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = *directoryID;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = 0;
- if (PBGetCatInfo(&pb, false) != noErr ||
- !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
- {
- return fnamebuff;
- }
- x = i + 1;
- atleastone = true;
- *directoryID = pb.hFileInfo.ioDirID;
- }
- }
- if (!atleastone) {
- i = strlen(pathname);
- }
- fnamebuff[0] = i - x;
- memcpy(&fnamebuff[1], &pathname[x], fnamebuff[0]);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = fnamebuff;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = *directoryID;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = 0;
- if (PBGetCatInfo(&pb, false) != noErr ||
- !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
- {
- return fnamebuff;
- }
- return NULL;
- }
-
- /*
- * unix make directory
- */
- int mkdir(char *path, int mode)
- {
- #pragma unused(mode)
-
- HParamBlockRec pb;
- char *nameptr;
- long directoryID;
-
- if ((nameptr = finddir(path, &directoryID)) != NULL) {
- pb.fileParam.ioCompletion = NULL;
- pb.fileParam.ioNamePtr = nameptr;
- pb.fileParam.ioVRefNum = gMudLibVolRef;
- pb.fileParam.ioDirID = directoryID;
- pb.fileParam.ioResult = noErr;
- pb.fileParam.ioFDirIndex = 0;
- return ((PBDirCreate(&pb, false) != noErr) * -1);
- }
- return (-1);
- }
-
- int rmdir(char *path)
- {
- CInfoPBRec pb;
- HParamBlockRec dirpb;
- short i, x;
- long directoryID, tid;
- Boolean atleastone = false;
-
- directoryID = tid = gMudLibDirID;
- for (i = 0, x = 0; i < strlen(path); i++) {
- if (path[i] == '/') {
- fnamebuff[0] = i - x;
- memcpy(&fnamebuff[1], &path[x], fnamebuff[0]);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = fnamebuff;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = tid;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = 0;
- if (PBGetCatInfo(&pb, false) != noErr ||
- !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
- {
- return (-1);
- }
- x = i + 1;
- atleastone = true;
- directoryID = tid;
- tid = pb.hFileInfo.ioDirID;
- }
- }
- if (!atleastone) {
- i = strlen(path);
- }
- directoryID = tid;
- fnamebuff[0] = i - x;
- memcpy(&fnamebuff[1], &path[x], fnamebuff[0]);
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = fnamebuff;
- pb.hFileInfo.ioVRefNum = gMudLibVolRef;
- pb.hFileInfo.ioDirID = directoryID;
- pb.hFileInfo.ioResult = noErr;
- pb.hFileInfo.ioFDirIndex = 0;
- if (PBGetCatInfo(&pb, false) != noErr ||
- !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
- {
- return (-1);
- }
- dirpb.fileParam.ioCompletion = NULL;
- dirpb.fileParam.ioNamePtr = fnamebuff;
- dirpb.fileParam.ioVRefNum = gMudLibVolRef;
- dirpb.fileParam.ioDirID = directoryID;
- dirpb.fileParam.ioResult = noErr;
- dirpb.fileParam.ioFDirIndex = 0;
- return ((PBHDelete(&dirpb, false) != noErr) * -1);
- }
-
- static short time_secs;
- static unsigned long time_ticks;
-
- void start_timer(short seconds)
- {
- time_secs = seconds;
- time_ticks = TickCount() + (unsigned long)time_secs * (unsigned long)60;
- }
-
- int timer_expired(void)
- {
- if (TickCount() >= time_ticks) {
- time_ticks = TickCount() + (unsigned long)time_secs * (unsigned long)60;
- return 1;
- } else return 0;
- }